package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.admin.service.ICourseService;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.validate.course.CourseCreateValidate;
import com.shop.admin.validate.course.CourseSearchValidate;
import com.shop.admin.validate.course.CourseUpdateValidate;
import com.shop.admin.vo.course.CourseDetailVo;
import com.shop.admin.vo.course.CourseListedVo;
import com.shop.common.core.PageResult;
import com.shop.common.entity.course.Course;
import com.shop.common.mapper.course.CourseMapper;
import com.shop.common.util.ListUtils;
import com.shop.common.util.OSSUtil;
import com.shop.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程实现类
 * @author DragonDean
 */
@Service
public class CourseServiceImpl implements ICourseService {
        
    @Resource
    CourseMapper courseMapper;

    /**
     * 课程列表
     *
     * @author DragonDean
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CourseListedVo>
     */
    @Override
    public PageResult<CourseListedVo> list(PageValidate pageValidate, CourseSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        // 按照分类筛选,用FIND_IN_SET筛选
        if(searchValidate.getCategoryId() != null) {
            queryWrapper.apply("FIND_IN_SET({0}, category_ids)", searchValidate.getCategoryId());
        }

        courseMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:title:str",
            "=:selected:int",
            "=:type:int",
        });

        IPage<Course> iPage = courseMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CourseListedVo> voList = new ArrayList<>();
        for(Course item : iPage.getRecords()) {
            CourseListedVo vo = new CourseListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            vo.setCategoryIds(ListUtils.stringToListAsInt(item.getCategoryIds(), ","));
            vo.setCover(OSSUtil.completeUrl(vo.getCover()));
            voList.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), voList);
    }

    /**
     * 课程详情
     *
     * @author DragonDean
     * @param id 主键参数
     * @return Course
     */
    @Override
    public CourseDetailVo detail(Integer id) {
        Course model = courseMapper.selectOne(
                new QueryWrapper<Course>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CourseDetailVo vo = new CourseDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setCategoryIds(ListUtils.stringToListAsInt(model.getCategoryIds(), ","));
        vo.setImages(ListUtils.stringToListAsStr(model.getImages(), ","));
        return vo;
    }

    /**
     * 课程新增
     *
     * @author DragonDean
     * @param createValidate 参数
     */
    @Override
    public void add(CourseCreateValidate createValidate) {
        Course model = new Course();
        model.setCategoryIds(ListUtils.listToStringByInt(createValidate.getCategoryIds()));
        model.setTitle(createValidate.getTitle());
        model.setSelected(createValidate.getSelected());
        model.setCover(createValidate.getCover());
        model.setIntro(createValidate.getIntro());
        model.setStudents(createValidate.getStudents());
        model.setStudyCount(createValidate.getStudyCount());
        model.setTime(createValidate.getTime());
        model.setType(createValidate.getType());
        model.setContent(createValidate.getContent());
        model.setImages(ListUtils.listToStringByStr(createValidate.getImages(), ","));
        model.setVideo(createValidate.getVideo());
        model.setAudienceType(createValidate.getAudienceType());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseMapper.insert(model);
    }

    /**
     * 课程编辑
     *
     * @author DragonDean
     * @param updateValidate 参数
     */
    @Override
    public void edit(CourseUpdateValidate updateValidate) {
        Course model = courseMapper.selectOne(
                new QueryWrapper<Course>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setCategoryIds(ListUtils.listToStringByInt(updateValidate.getCategoryIds()));
        model.setTitle(updateValidate.getTitle());
        model.setSelected(updateValidate.getSelected());
        model.setCover(updateValidate.getCover());
        model.setIntro(updateValidate.getIntro());
        model.setStudents(updateValidate.getStudents());
        model.setStudyCount(updateValidate.getStudyCount());
        model.setTime(updateValidate.getTime());
        model.setType(updateValidate.getType());
        model.setContent(updateValidate.getContent());
        model.setImages(ListUtils.listToStringByStr(updateValidate.getImages(), ","));
        model.setVideo(updateValidate.getVideo());
        model.setAudienceType(updateValidate.getAudienceType());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        courseMapper.updateById(model);
    }

    /**
     * 课程删除
     *
     * @author DragonDean
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Course model = courseMapper.selectOne(
                new QueryWrapper<Course>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        courseMapper.updateById(model);
    }

}
